package com.Shoping.dao.impl;

import com.Shoping.dao.AddressDao;
import com.Shoping.entity.Address;
import com.Shoping.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl implements AddressDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public Integer addAddress(Address address) throws SQLException {
        String sql = "INSERT INTO address(u_id,a_name,a_phone,a_address) VALUES(?,?,?,?);";
        return queryRunner.update(DbUtils.getConnection(),sql,address.getuId(),address.getaName(),address.getaPhone(),address.getaAddress());
    }

    @Override
    public List<Address> selectByUid(Integer uId) throws SQLException {
        String sql = "SELECT a_id aId,u_id uId,a_name aName,a_phone aPhone,a_address aAddress,a_coding aCoding,a_serial aSerial FROM address WHERE u_id=?;";
        return queryRunner.query(DbUtils.getConnection(),sql,new BeanListHandler<>(Address.class),uId);
    }
}
